
const express = require("express")
const app = express();
const path = require('path');
//处理body请求体的中间件
app.use(express.urlencoded())
app.use(express.json())
app.get('/user',(requste,response)=>{
    const {userId}=requste.query;
      //如果查询失败 响应
    if(userId !=='001'){
        return response.send({
            code:10001,
            msg:'查询失败',
            data:null
        })
    }
     //查询成功 响应  
    response.send({
        code:10000,
        msg:'查询成功',
        data:{
            name: "李为",
            age: 18,
            sex: "男"
        }
    })
})
app.post('/adduser',(request,response)=>{
    const{
        name,
        age,
        sex
    }=request.body
    if(!name || !age || !sex){
        return response.send({
            code:10001,
            mes:'添加失败',
            data:null
        })
    }
    response.send({
        code:10000,
        mes:'添加成功',
        data:null
    })
})
app.put("/updateuser", (req, res) => {
    const {
      userId,
      age,
      sex
    } = req.body;
  
    console.log(userId, age, sex, "更新");
  
    res.send({
      code: 10000,
      mes: "更新成功",
      data: null
    })
  })
  
app.delete('/deleteuser',(request,response)=>{
    const {userId}= request.query
    response.send({
        code:10000,
        mes:'删除成功',
        data:null
    })
})
//静态资源部署接口
app.get('/1',(request,response)=>{
    const filePath = path.resolve(__dirname,"./01.axios的基础.html")
    response.sendFile(filePath);
})
app.get('/2',(request,response)=>{
    const filePath = path.resolve(__dirname,'./02.axios请求别名.html');
    response.sendFile(filePath);
})
app.listen(5000,()=>{
    console.log("服务器请求成功 请访问 http://127.0.0.1:5000");
})